# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Maintainer: Robin Candau <antiz@archlinux.org>
# Contributor: Christian Muehlhaeuser <muesli at gmail dot com>
# Contributor: Stephen Gregoratto <dev@sgregoratto.me>

pkgname=glow
pkgver=1.5.1
pkgrel=1
pkgdesc='Command-line markdown renderer'
arch=('loong64' 'x86_64')
url='https://github.com/charmbracelet/glow'
license=('MIT')
depends=('glibc')
makedepends=('git' 'go')
options=('!lto')
_commit='ad21129703d0bd48a9b45227f56d2737623db57b'
source=("$pkgname::git+$url.git#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # create directory for build output
  mkdir build

  # download dependencies
  go mod download
}

build() {
  cd "$pkgname"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"

  go build -v \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags ${LDFLAGS} \
    -X main.Version=$pkgver \
    -X main.CommitSHA=$_commit" \
    -o build \
    .

  # generate shell completions
  build/glow completion bash > completion.bash
  build/glow completion fish > completion.fish
  build/glow completion zsh > completion.zsh
}

package() {
  cd "$pkgname"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" build/glow

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE

  # shell completions
  install -vDm644 completion.bash "$pkgdir/usr/share/bash-completion/completions/glow"
  install -vDm644 completion.fish "$pkgdir/usr/share/fish/vendor_completions.d/glow.fish"
  install -vDm644 completion.zsh "$pkgdir/usr/share/zsh/site-functions/_glow"
}

# vim:set ts=2 sw=2 et:
